// pages/published-order/published-order.js
import { UserInfo } from "../../models/userinfo";
import { Home } from "../../models/home";
import { Demand } from "../../models/demand";
import Toast from "../../miniprogram_npm/@vant/weapp/toast/toast";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    productList: [],
    PageIndex: 1,
    PageRows: 10,
    userInfo: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let data = new UserInfo();
    this.data.userInfo = data.getUserInfo();
    this.setData({
      userInfo: data.getUserInfo()
    });
    this.init();
  },
  async init() {
    let postData =
      // {
      //   "PageIndex": this.PageIndex,
      //   "PageRows": this.PageRows,
      //   "SortField": "id",
      //   "SortType": "asc"
      // }

      {
        Search: { all: false, userId: this.data.userInfo.Id, keyword: null },
        PageIndex: 1,
        PageRows: 10000,
        SortField: "id",
        SortType: "desc"
      };
    const data = await Home.GetDataList(postData);
    data.Data.map(item => {
      item.imageURL = item.Imags.split(",")[0];
      item.time = item.CreateTime.split(" ")[0];
    });

    this.setData({
      productList: data.Data
    });
    // this.productList = data.Data;
    console.log(data);
  },
  edit(e) {
    let id = e.currentTarget.dataset.id;
    console.log(id);
    wx.navigateTo({
      url: `/pages/publish-order/publish-order?id=${id}`
    });
  },
  async del(e) {
    let id = e.currentTarget.dataset.id;
    const data = await Demand.DeleteData([id]);
    if (data.Success) {
      Toast("删除成功");
      this.init();
    } else {
      Toast(data.Msg);
    }
  },
  async finish(e) {
    let id = e.currentTarget.dataset.id;
    let index = e.currentTarget.dataset.index;
    let postData = this.data.productList[index];
    postData.Status = 2;
    const data = await Home.SaveData(postData);
    if (data.Success) {
      Toast("修改成功");
      this.init();
    } else {
      Toast(data.Msg);
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {}
});
